<?php
namespace App\Admin\Controllers\Inventory;

use App\Admin\Ajax\Inventory\TransferPAjax;
use App\Models\Inventory\AreaCn;
use App\Models\Inventory\TransferP;
use App\Models\Product\ProductClassify;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Layout\Content;
use Illuminate\Support\Facades\Request;
class TransferPController extends AdminController{

    protected $title = '公共库存挪用';

    protected $filters = [
        'sku' => null,
        'season' => null,
        'classify_id' => null,
    ];

    protected function grid()
    {
        $filtersField = array_keys($this->filters);
        $filters = $this->_getFilters($filtersField);
        $this->_initFilters($filters);
        $query = $this->_initQuery($filters);
        $_query = clone $query;
        $paginator = $this->_paginator($query);
        $total = $this->_sum($_query);
        $action = getAction($filtersField);
        $filtersBlade = [];
        $filtersBlade['sku'] = $this->filters['sku'];
        $filtersBlade['season'] = getOptions(ProductClassify::$_season,$this->filters['season']);
        $filtersBlade['classify_id'] = getOptions(ProductClassify::options(),$this->filters['classify_id']);
        return view('inventory.transfer-p.index', compact('paginator','total','filtersBlade','action'));
    }

    private function _getFilters($filtersField){
        $filters = [];
        foreach ($filtersField as $field){
            $filters[$field] = request($field);
        }
        return $filters;
    }

    private function _initFilters($filters){
        foreach ($filters as $field => $filter){
            $this->filters[$field] = $filter;
        }
    }

    private function _initQuery($filters){
        $query = TransferP::query()
            ->whereHas('area', function ($query){
                $query->where('area', 'PUBLIC');
            })->orderBy('psku_id', 'DESC');
        foreach ($filters as $field => $filter){
            if(!$filter)continue;
            if($field == 'sku'){
                $query->whereHas('sku', function ($query) use($filter){
                    $query->where('psku_code', 'like', "%{$filter}%")
                        ->orWhere('psku_cn', 'like', "%{$filter}%");
                });
            }
            if($field == 'classify_id'){
                $query->whereHas('sku', function ($query) use($filter){
                    $query->where('classify_id', $filter);
                });
            }
            if($field == 'season'){
                $query->whereHas('sku.classify', function ($query) use($filter){
                    $query->where('season', $filter);
                });
            }
        }
        return $query;
    }

    private function _paginator($query){
        return $query->paginate((int) (request("per_page",10)))->appends(Request::input());
    }

    private function __getSumFields($groupBy){
        $re = '';
        $re .= 'sum(instant) as instant,';
        $re .= 'sum(transit) as transit,';
        $re .= 'sum(reserve) as reserve,';
        return $re.$groupBy;
    }

    private function __getSumFieldsInit(){
        return [
            'instant' => 0,
            'transit' => 0,
            'reserve' => 0
        ];
    }

    private function _sum($query){
        $sum = $query->selectRaw($this->__getSumFields(1))->groupBy(1)->first();
        return $sum ? $sum->toArray() : $this->__getSumFieldsInit();
    }

    public function ajax(){
        $action = request('action');
        $param = request('param');
        $ajax = new TransferPAjax();
        return $param ? call_user_func([$ajax,$action],$param) : call_user_func([$ajax,$action]);
    }

    public function modal(){
        $action = request('action');
        $param = request('param');
        $modal = new TransferPAjax();
        return $param ? call_user_func([$modal,$action],$param) : call_user_func([$modal,$action]);
    }

    protected function confirm(Content $content)
    {
        $id = request('id');
        $iAreaId = request('iarea_id');
        $deductTransit = request('deduct_transit');
        $transferP = TransferP::query()->find($id);
        $transit = AreaCn::getTransit($iAreaId,$transferP->psku_id);
        return $content
            ->title($this->title())
            ->description('挪用')
            ->body(view('inventory.transfer-p-confirm.index', compact('id','transferP','transit','iAreaId','deductTransit')));
    }
}
